﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Customization;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration.Assemblies;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SEPD.CAD.Template.Palette
{
    public class TemplateCommand
    {

        /*
        两个命令方法 都可以被启动  
        ShowPaletteSet直接让侧边栏显示  
        cui相关命令则是将侧边栏启动的按钮放到右键菜单
        请择优使用 记得修改json签名
         */

        /// <summary>
        /// 可以直接用命令显示启动显示  
        /// </summary>
        [CommandMethod("ShowPaletteSet")]
        public void ShowPaletteSet()
        {
            AddMyPaletteSet();
        }

        /// <summary>
        /// 将侧边栏启动的按钮放到右键菜单
        /// </summary>
        [CommandMethod("CmdCui")]
        public void CmdCui()
        {
            //自定义组名   or cuiSectionName
            string cuiSectionName = "PaletteSetTools";
             
            //创建CUI文件
            string cuixFile = TemplateService.CreateCuix(cuiSectionName);
            //先删除旧的菜单
            TemplateService.UnloadCuix(new List<string>() { });
            //判断当前要加载的cuix是否已经加载过了
            bool isExist = TemplateService.IsCuixExist(cuiSectionName);
            if (isExist) {
                //删除旧的菜单
                TemplateService.UnloadCuix(new List<string>() { cuiSectionName }); 
            }
            //加载CUI文件
            TemplateService.LoadCuix(cuixFile);
            //右键菜单
            Dictionary<string,EventHandler> dict = new Dictionary<string, EventHandler>();
            dict.Add("ShowDockpanel1", (sender, args) => { AddMyPaletteSet(); });
            dict.Add("ShowDockpanel2", (sender, args) => { AddMyPaletteSet(); });
            TemplateService.MouseRightButtonMenuAttach(cuiSectionName, dict);


            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"CMDCUI EXEC DONE\n");
        }


        //====================================================================
        /// <summary>
        /// 加载并显示palette停靠栏
        /// </summary>
        public void AddMyPaletteSet()
        {
            Autodesk.AutoCAD.Windows.PaletteSet paletteSet
                = new Autodesk.AutoCAD.Windows.PaletteSet("我的PallteSet");

            paletteSet.Dock = DockSides.Right;
            paletteSet.MinimumSize = new System.Drawing.Size(400, 200);
            paletteSet.Size = new System.Drawing.Size(400, 200);
            paletteSet.Opacity = 50;

            //启用设计好的界面空间选项卡
            TemplatePaletteSetView templatePaletteSetView = new TemplatePaletteSetView();
            templatePaletteSetView.DataContext = new TemplatePaletteSetViewModel();
            paletteSet.AddVisual("通用", templatePaletteSetView);

            //普通备用选项卡
            paletteSet.Add("我的工具集", new System.Windows.Forms.Control("我的工具集"));
            paletteSet.Add("我的颜色", new System.Windows.Forms.Control("我的颜色"));
            paletteSet.Add("我的控件", new System.Windows.Forms.Control("我的控件"));



            paletteSet.Visible = true;
        }



    }
}
